﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Views.Admin;
using NLBS.Entity;
using NLBS.Views;

namespace NLBS.Web.UI
{
    public class ExtensionLinkCommand : ICommand, IAction<ExtensionLink>
    {
        #region Presenter

        private ExtensionLinkPresenter _presenter;

        public ExtensionLinkPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new ExtensionLinkPresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion

        private Action act;

        private string[] parameters;
        public ExtensionLinkCommand(Action act, string[] parameters)
        {
            this.act = act;
            this.parameters = parameters;
        }

        #region ICommand Members

        public void Excute()
        {
            PresenterInit();
            switch (act)
            {
                case Action.Insert:
                    UpdateAction();
                    if (parameters.Length > 6)
                    {
                        InsertAction();
                    }
                    break;
                case Action.Delete:
                    DeleteAction();
                    break;
            }
        }

        private void DeleteAction()
        {
            for (int i = 0; i < parameters.Length; i++)
            {
                if (i > 0)
                {
                    this.keyID = Convert.ToInt32(parameters[i]);
                    this.DataDelete(null, null);
                }
            }
        }

        private void InsertAction()
        {
            string[] newword = parameters[6].Split(new char[] { ',' });
            ExtensionLink link = new ExtensionLink();
            link.LinkName = newword[0];
            link.LinkURL = newword[1];
            link.Extensions = newword[2];
            link.Order =Convert.ToInt32(newword[3]);
            link.Type = 1;
            this.Entity = link;
            this.DataInsert(null, null);
        }

        private void UpdateAction()
        {
            int u = parameters[1].IndexOf(",");
            if (u > 0)
            {
                string[] nameList = parameters[2].Split(new char[] { ',' });
                string[] urlList = parameters[3].Split(new char[] { ',' });
                string[] extensionsList = parameters[4].Split(new char[] { ',' });
                string[] orderList = parameters[5].Split(new char[] { ',' });
                string[] linkList = parameters[1].Split(new char[] { ',' });
                for (int w = 0; w < linkList.Length; w++)
                {
                    this.keyID = Convert.ToInt32(linkList[w]);
                    this.DataSelectSingle(null, null);
                    ExtensionLink updatelink = this.Entity;
                    updatelink.LinkName = nameList[w];
                    updatelink.LinkURL = urlList[w];
                    updatelink.Extensions = extensionsList[w];
                    updatelink.Order = Convert.ToInt32(orderList[w]);
                    this.DataUpdate(null, null);
                }
            }
        }

        #endregion

        #region IAction<ExtensionLink> Members
        private ExtensionLink _entity;
        public ExtensionLink Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }


        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataSelectSingle;

        #endregion
    }
}
